package com.rizzo.android.ui.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import com.rizzo.android.ui.fragment.RizzoFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: jalie
 * Date: 12/19/12
 * Time: 6:06 PM
 * To change this template use File | Settings | File Templates.
 */
public class MainPagerAdapter extends FragmentPagerAdapter {

    private List<Holder> holderList = new ArrayList<Holder>();
    private FragmentActivity fragmentActivity;

    public void addFragment(int position, Class fragment) {
        Bundle params = new Bundle();
        params.putInt(RizzoFragment.POSITION_STATE, position);
        this.add(fragment, params);
    }

    public MainPagerAdapter(FragmentActivity fragmentActivity) {
        super(fragmentActivity.getSupportFragmentManager());
        this.fragmentActivity = fragmentActivity;
        Log.d(this.getClass().getCanonicalName(), "Constructor");
    }

    private void add(Class<? extends Fragment> className, Bundle params) {
        Holder holder = new Holder();
        holder.className = className.getName();
        holder.params = params;

        int position = holderList.size();
        holderList.add(position, holder);
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        Holder currentHolder = holderList.get(position);
        return Fragment.instantiate(fragmentActivity, currentHolder.className, currentHolder.params);
    }

    @Override
    public int getCount() {
        return holderList.size();
    }

    private class Holder {
        String className;
        Bundle params;
    }

}
